package com.zy.xuanwo.acitivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zy.xuanwo.R;
import com.zy.xuanwo.utils.RegexUtils;
import com.zy.xuanwo.view.CleanEditText;
import com.zy.xuanwo.zyc.portal.bean.ErrorMessag;
import com.zy.xuanwo.zyc.portal.callback.GetCommonResultListener;
import com.zy.xuanwo.zyc.portal.service.imp.PortalMagger;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @功能 忘记密码
 * Created by njb on 2016/7/8 0008.
 */
public class SurePassWordActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.edit_new_password)
    CleanEditText editNewPassword;
    @Bind(R.id.edit_current_password)
    CleanEditText editCurrentPassword;
    @Bind(R.id.btn_sure)
    Button btnSure;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_forgetpwd_sure);
        ButterKnife.bind(this);
    }

    private void login() {
        String s = null;
        String username = editCurrentPassword.getText().toString().trim();
        String password = editNewPassword.getText().toString().trim();
        String surepassword = editNewPassword.getText().toString().trim();
        String verification_code = editNewPassword.getText().toString().trim();

        if (checkInput(username, password,surepassword)) {

            PortalMagger.getInstance(SurePassWordActivity.this).usersPasswordReset(username, password, getheaderMap(),
                    new GetCommonResultListener() {
                        @Override
                        public void onSuccess(String result) {
                            startActivity(new Intent(SurePassWordActivity.this,LoginActivity.class));
                            SurePassWordActivity.this.finish();
                        }

                        @Override
                        public void onPrepare(String cancelHandler) {

                        }

                        @Override
                        public void onExection(Exception e) {

                        }

                        @Override
                        public void onFail(ErrorMessag msg) {
                        Toast.makeText(SurePassWordActivity.this,"",Toast.LENGTH_SHORT).show();
                        }
                    });

        }
    }

    private HashMap<String, String> getheaderMap() {
        HashMap<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("token", "446f8fd7-8fc3-426d-8c70-283c021dd380");
        headerMap.put("X-Request-Sign", "f033abaf9eac94ffc03d25ee6be445e3, 1467598209");
        headerMap.put("X-Application-Id", "3d632345-abd5-4c7c-b157-6d12cd3f0dde");
        return headerMap;
    }

    private boolean checkInput(String username, String password, String surepassword) {
        Log.e("TAG", "Checkout");
        if (TextUtils.isEmpty(username)) { // 电话号码为空
            Toast.makeText(this, "用户名不能为空",
                    Toast.LENGTH_SHORT).show();
        } else {
            if (!RegexUtils.checkMobile(username)||!RegexUtils.checkEmail(username)) { // 电话号码格式有误
                Toast.makeText(this, "请您输入正确的手机号或者邮箱",
                        Toast.LENGTH_SHORT).show();
            }
            if (password.length() < 6 || password.length() > 20
                    || TextUtils.isEmpty(password)) { // 密码格式
                Toast.makeText(this, "请您输入密码，并且长度不能超过20",
                        Toast.LENGTH_SHORT).show();
            } else if (surepassword.length() < 6 || editNewPassword.length() > 20
                    || TextUtils.isEmpty(surepassword)) { // 确认密码格式
                Toast.makeText(this, "请您输入确认密码，并且长度不能超过20",
                        Toast.LENGTH_SHORT).show();
            }
            else {
                Log.e("TAG", "CheckoutTrue");
                return true;
            }
        }
        return false;
    }


    @OnClick({R.id.btn_sure,R.id.left_sliding_menu_iv})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_sliding_menu_iv:
                startActivity(new Intent(SurePassWordActivity.this,ForgetPassWordActivity.class));
                this.finish();
                break;
            case R.id.btn_sure:
                login();
                break;
        }
    }


}
